﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Citrix.WorkflowStudio.Activities.WFSDatabaseActivities.PropertyEditorUI
{
	public partial class SQLColumnEditor: Form
	{
        List<Classes.SQLColumn> pColumnList = null;
        
        //public delegate void EventPassBackClass(List<Classes.SQLColumn> value);
        //public event EventPassBackClass GetColumns;

        public List<Classes.SQLColumn> Columns { get; set; }

        private void BuildDropDownList(DataGridViewComboBoxCell ComboObjectUI)
        {
            ComboObjectUI.Items.Add("bigint");
            ComboObjectUI.Items.Add("decimal");
            ComboObjectUI.Items.Add("int");
            ComboObjectUI.Items.Add("numeric");
            ComboObjectUI.Items.Add("smallint");
            ComboObjectUI.Items.Add("money");
            ComboObjectUI.Items.Add("tinyint");
            ComboObjectUI.Items.Add("smallmoney");
            ComboObjectUI.Items.Add("bit");
            ComboObjectUI.Items.Add("float");
            ComboObjectUI.Items.Add("real");
            ComboObjectUI.Items.Add("datetime");
            ComboObjectUI.Items.Add("smalldatetime");
            ComboObjectUI.Items.Add("char");
            ComboObjectUI.Items.Add("text");
            ComboObjectUI.Items.Add("varchar");
            ComboObjectUI.Items.Add("nchar");
            ComboObjectUI.Items.Add("ntext");
            ComboObjectUI.Items.Add("nvarchar");
            ComboObjectUI.Items.Add("binary");
            ComboObjectUI.Items.Add("image");
            ComboObjectUI.Items.Add("varbinary");
            ComboObjectUI.Items.Add("timestamp");
        }
        private void BuildDropDownList(DataGridViewComboBoxColumn ComboObjectUI )
        {
            ComboObjectUI.Items.Add("bigint");
            ComboObjectUI.Items.Add("decimal");
            ComboObjectUI.Items.Add("int");
            ComboObjectUI.Items.Add("numeric");
            ComboObjectUI.Items.Add("smallint");
            ComboObjectUI.Items.Add("money");
            ComboObjectUI.Items.Add("tinyint");
            ComboObjectUI.Items.Add("smallmoney");
            ComboObjectUI.Items.Add("bit");
            ComboObjectUI.Items.Add("float");
            ComboObjectUI.Items.Add("real");
            ComboObjectUI.Items.Add("datetime");
            ComboObjectUI.Items.Add("smalldatetime");
            ComboObjectUI.Items.Add("char");
            ComboObjectUI.Items.Add("text");
            ComboObjectUI.Items.Add("varchar");
            ComboObjectUI.Items.Add("nchar");
            ComboObjectUI.Items.Add("ntext");
            ComboObjectUI.Items.Add("nvarchar");
            ComboObjectUI.Items.Add("binary");
            ComboObjectUI.Items.Add("image");
            ComboObjectUI.Items.Add("varbinary");
            ComboObjectUI.Items.Add("timestamp");
        }
		public SQLColumnEditor(List<Classes.SQLColumn> ColumnList)
        //public SQLColumnEditor()
		{
			InitializeComponent();

            this.dataGridView1.AutoGenerateColumns = false;
            
            DataGridViewTextBoxColumn colName = new DataGridViewTextBoxColumn();
            colName.DataPropertyName = "Name";
            this.dataGridView1.Columns.Add(colName);

            DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
            this.BuildDropDownList(cmb);
            this.dataGridView1.Columns.Add(cmb);

            DataGridViewTextBoxColumn colLength = new DataGridViewTextBoxColumn();
            colName.DataPropertyName = "Name";
            this.dataGridView1.Columns.Add(colLength);


            pColumnList = ColumnList;

            if (pColumnList != null)
            {
                foreach (Classes.SQLColumn col in pColumnList)
                {
                    System.Windows.Forms.DataGridViewRow r = new DataGridViewRow();
                    int rowindex = dataGridView1.Rows.Add(r);
                    dataGridView1.Rows[rowindex].Cells[0].Value = col.Name;
                    dataGridView1.Rows[rowindex].Cells[1].Value = col.Type;
                    if (col.Length != null)
                    {
                        dataGridView1.Rows[rowindex].Cells[2].Value = col.Length;
                    }

                    System.Windows.Forms.DataGridViewComboBoxCell cb = (System.Windows.Forms.DataGridViewComboBoxCell)dataGridView1.Rows[rowindex].Cells[1];
                    this.BuildDropDownList(cb);
                    cb.Value = col.Type;
                }
            }
            else
            {
                pColumnList = new List<Citrix.WorkflowStudio.Activities.WFSDatabaseActivities.Classes.SQLColumn>();
            }
		}

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            this.dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
            this.pColumnList.Clear();

            foreach (System.Windows.Forms.DataGridViewRow row in this.dataGridView1.Rows)
            {
                if (!row.IsNewRow)
                {
                    Classes.SQLColumn sqlColumn = new Citrix.WorkflowStudio.Activities.WFSDatabaseActivities.Classes.SQLColumn();
                    sqlColumn.Name = row.Cells[0].Value.ToString();
                    sqlColumn.Type = row.Cells[1].Value.ToString();
                    if (row.Cells[2].Value != null)
                    {
                        sqlColumn.Length = row.Cells[2].Value.ToString();
                    }
                    this.pColumnList.Add(sqlColumn);
                }
            }
            
            this.Columns = this.pColumnList;
            this.Close();

        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            this.Columns = this.pColumnList;
            this.Close();
        }

	}
}
